package com.foreknow.elm.controller;

import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressController {
    private DeliveryAddressService service = new DeliveryAddressServiceImpl();
    /**
     *  功能：根据用户编号查询所属送货地址
     * @param request
     * @return
     * @throws Exception
     */
    public Object listDeliveryAddressByUserId(HttpServletRequest request)throws Exception{
        List<Deliveryaddress> list = new ArrayList<>();
        String userId = request.getParameter("userId");
        list = service.listDeliveryAddressByUserId(userId);
        return list;
    }

    /**
     *  功能：根据送货地址编号查询送货地址
     * @param request
     * @return
     * @throws Exception
     */
    public Object getDeliveryAddressById(HttpServletRequest request)throws Exception{
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        Deliveryaddress dls = service.getDeliveryAddressById(daId);
        return dls;
    }

    /**
     * 功能：向送货地址表中添加一条记录
     * @param request
     * @return
     * @throws Exception
     */
    public Object saveDeliveryAddress(HttpServletRequest request)throws Exception{
        Deliveryaddress dls= new Deliveryaddress();
        dls.setContactName(request.getParameter("contactName"));
        dls.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        dls.setContactTel(request.getParameter("contactTel"));
        dls.setAddress(request.getParameter("address"));
        dls.setUserId(request.getParameter("userId"));
        Integer result = service.saveDeliveryAddress(dls);
        return result;
    }

    /**
     * 功能：根据送货地址编号更新送货地址信息
     * @param request
     * @return
     * @throws Exception
     */
    public Object updateDeliveryAddress(HttpServletRequest request)throws Exception{
        Deliveryaddress dls= new Deliveryaddress();
        dls.setContactName(request.getParameter("contactName"));
        dls.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        dls.setContactTel(request.getParameter("contactTel"));
        dls.setAddress(request.getParameter("address"));
        dls.setUserId(request.getParameter("userId"));
        dls.setDaId(Integer.valueOf(request.getParameter("daId")));
        Integer result = service.updateDeliveryAddress(dls);
        return result;
    }

    /**
     * 功能：根据送货地址编号删除一条记录
     * @param request
     * @return
     * @throws Exception
     */
    public Object removeDeliveryAddress(HttpServletRequest request)throws Exception{
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        Integer result = service.removeDeliveryAddress(daId);
        return result;
    }
}
